﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Globalization;
using System.Linq;
using System.Resources;
using System.Threading;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Ryder_US_Website.BUS;
using Ryder_US_Website.Utils;

namespace Ryder_US_Website.Presentation.Guest
{
    public partial class ThongTin : System.Web.UI.Page
    {
        protected RUS_Customer cus = null;
        ResourceManager rm;
        CultureInfo ci;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (Session["RUS_Customer"] == null)
            {
                Response.Redirect("TrangChu.aspx");
            }

            if (Session["Location"] == null)
            {
                Session["Location"] = "vi-VN";
            }
            Thread.CurrentThread.CurrentCulture = new CultureInfo(Session["Location"].ToString());
            rm = new ResourceManager("Resources.SitMap", System.Reflection.Assembly.Load("App_GlobalResources"));
            ci = Thread.CurrentThread.CurrentCulture;
            LoadString(ci);
            Session["Lang"] = Session["Location"].ToString().Substring(0, 2);
            chbMatKhau.Checked = false;
            cus = (RUS_Customer)Session["RUS_Customer"];
            if (!IsPostBack)
                fillForm();
        }

        protected void fillForm()
        {
            lblUsername.Text = cus.Customer_username;
            txtHoTen.Text = cus.Customer_name;
            LabelKhoanNo.Text = String.Format("{0:0}", cus.Customer_doubt);
            txtDiaChi.Text = cus.Customer_address;
            txtDienThoai.Text = cus.Customer_phone;
            txtTaiKhoan.Text = cus.Customer_banking;
        }

        protected void btnCapNhat_Click(object sender, EventArgs e)
        {
            try
            {
                String fullname = txtHoTen.Text;
                String address = txtDiaChi.Text;
                String phone = txtDienThoai.Text;
                String banking = txtTaiKhoan.Text;
                String password = cus.Customer_password;
                if (chbMatKhau.Checked)
                {
                    password = UtilHelpers.MD5Encode(txtMatKhau.Text);
                    CustomerBUS.Update(cus.Customer_ID, fullname, cus.Customer_username, password, address, phone,
                                       banking, cus.Customer_doubt, cus.Customer_trash);
                }
                else
                {
                    CustomerBUS.Update(cus.Customer_ID, fullname, cus.Customer_username, address, phone, banking,
                                       cus.Customer_doubt, cus.Customer_trash);
                }
                Session["RUS_Customer"] = cus;
            }
            catch (Exception ex)
            {
                UtilHelpers.ShowMessage(ex.Message);
            }
            Page_Load(sender, e);
        }

        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("TrangChu.aspx");
        }

        public void LoadString(CultureInfo ci)
        {
            lbDiaChi.Text = rm.GetString("Address", ci);
            lbDienThoai.Text = rm.GetString("Phone", ci);
            lbHoTen.Text = rm.GetString("Fullname", ci);
            lbKhoanNo.Text = rm.GetString("Debt", ci);
            lbTenDangNhap.Text = rm.GetString("Username", ci);
            lbUpdateInfo.Text = rm.GetString("UpdateInfo", ci);
            chbMatKhau.Text = rm.GetString("ChangePass", ci);
            lbTaiKhoan.Text = rm.GetString("Account", ci);
            btnCapNhat.Text = rm.GetString("Update", ci);
            btnHuy.Text = rm.GetString("Cancel", ci);
        }
    }
}
